/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import DAO.LoginDAO;
import DAO.PrivilegioDAO;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Login;
import model.Mensagem;
import model.Privilegio;

/**
 *
 * @author Alex
 */
public class Controller_privilegio extends HttpServlet {

    public void definir_privilegio(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ArrayList<Object> arrobj = new ArrayList<Object>();

        Mensagem objmsg = new Mensagem();

        Gson objJson = new Gson();

        try {
            Privilegio privilegio = new Privilegio();            
            
            Login login = new Login();
            System.out.println("id do login em privilégios "+ request.getParameter("id"));
            login.setId(Integer.parseInt(request.getParameter("id")));
            
            privilegio.setLogin(login);
            privilegio.setAdministrador(Boolean.parseBoolean(request.getParameter("administrador")));
            privilegio.setAtendente(Boolean.parseBoolean(request.getParameter("atendente")));
            privilegio.setInstrutor(Boolean.parseBoolean(request.getParameter("instrutor")));
            privilegio.setAluno(Boolean.parseBoolean(request.getParameter("aluno")));


            PrivilegioDAO dao = new PrivilegioDAO();



            if (dao.definirPrivilegio(privilegio) == true) {
                objmsg.setMensagem("Cliente Alterado com Sucesso!");
            } else {
                objmsg.setMensagem("Algo não previsto aconteceu, favor verificar se o Cliente foi cadastrado!");
            }

            arrobj.add(objmsg);

            out.print(objJson.toJson(arrobj));

        } catch (Exception e) {

            //perceba que a mensagem é gerada na controladora

            objmsg.setMensagem("Algo não previsto aconteceu, favor verificar se o Cliente foi cadastrado!");
            arrobj.add(objmsg);

            out.print(objJson.toJson(arrobj));

        } finally {
            out.close();
        }

    }

    public void abastecer_campos(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        PrintWriter out = response.getWriter();
        Gson objJson = new Gson();
        PrivilegioDAO dao = new PrivilegioDAO();

        ArrayList<Privilegio> arrobj = new ArrayList<Privilegio>();


        try {

            arrobj.add(dao.buscarPrivilegioByCodigo(Integer.parseInt(request.getParameter("id"))));
            out.print(objJson.toJson(arrobj));

        } catch (Exception e) {
            System.out.println("Erro ao buscar por ID o login");
        } finally {
            out.close();
        }

    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int oper = Integer.parseInt(request.getParameter("oper"));
        try {
            switch (oper) {

                case 1:
                    definir_privilegio(request, response);
                    break;
                case 2:
                    abastecer_campos(request, response);
                    break;

            }

        } catch (Exception e) {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
